﻿using ComLib.ComType;
using ComUIControl;
using CoreBroker;
using FastReport;
using FastReport.Barcode;
using FastReport.Data;
using KInterfaces;
using KWindow.KPageCore;
using LogLib;
using Newtonsoft.Json.Linq;
using PhotoNamingService;
using System;
using System.IO;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PagePrintReceipt : PageControlBase
    {    
        private TakeReceiptOption _option = new TakeReceiptOption();

        public PagePrintReceipt()
        {           
            InitializeComponent();
            SetProcessBase(ProcessPrintReceipt.Instance);
            LoadOption();
        }

        private void LoadOption()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        /// <summary>
        /// 获取控件
        /// </summary>
        private void GetControls()
        {
           
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();          
        }

      
        //初始化窗体数据
        public override void InitData()
        {
            base.InitData();
            buttonNext.Visible = false;
            buttonNext.Enabled = false;                             
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();               
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
           base.Close();
        }

        public override void StartWork()
        {
            base.StartWork();
            DoPrinting();      
        }

        private void DoPrinting()
        {
            buttonNext.Visible = true;
            buttonNext.Enabled = true;
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        private void Go()
        {
            if (UserAction == UserApplyAction.TakePhotoAndWeiYinTB
             || UserAction == UserApplyAction.TakePhotoAndYSL)
            {
                Log.WriteAsync("进入微信预约填表模块.");
                Log.WriteAsync("下一页2:" + UserAction.ToString());
                Process.ToAny("YSLPages.PageQueryPeopleInfo");
            }
            else
            {
                Log.WriteAsync("拍照流程结束:" + UserAction);
                Process.ToHome();
            }
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            Log.WriteAsync("OnCountdownCompleted1:" + UserAction.ToString()); 
            if (!Process.IsRunning)
            {             
                ClearData();
                //Process.ToHome();   
                return;
            }
            UpdateData();
            NotifyClientTakePhotoFinished();
            ClearData();
            Go();
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            StopCountdown();
            UpdateData();
            NotifyClientTakePhotoFinished();
            ClearData();
            Go();
        }
		
		private void NotifyClientTakePhotoFinished()
        {
            TakenPhoto thePhoto = Process.GetValue<TakenPhoto>("TakenPhoto");
            JObject args = new JObject();          
            args["PhotoName"] = Process.GetValue<string>("PhotoSerialNumber");
            args["OrigPhotoPath"] = thePhoto.SourceImagePath;
            args["DestPhotoPath"] = thePhoto.DestImagePath;
            if(Process.Job != null)
            {
                Process.Job.OnFinished(args.ToString());
            }
        }
    }
}